<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" type="text/css" media="screen" href="css/redmond/jquery-ui-1.10.3.custom.min.css" />	<!-- jqueryUI CSS -->
<link rel="stylesheet" type="text/css" media="screen" href="css/selfTemplate.css" />	<!-- 本專案自行定義 CSS -->

<script src="js/jquery-2.1.0.min.js" type="text/javascript"></script>			<!-- jquery 函式庫 -->
<script src="js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>	<!-- jqueryUI 核心 -->
<script src="js/jquery.cookie.js" type="text/javascript"></script>				<!-- 操作cookie用 -->
<script src="js/widgets.js" type="text/javascript"></script> 					<!-- 本專案自行定義 JS -->

 
<script type="text/javascript">

function removeReq(obj) {
	$(obj).parent().parent().remove();
	refreshUI(1,"tbodyReqs",true);
}

function changeTargetSys() {
	//取得交付對象下相關的子系統
	$.ajax({type:"post",url:"QuerySys2Subsys",dataType: "json",async:false,
		data:{sysSN:$("#targetSystem option:selected").val()},
		success : function(reses) {
			$("select#subsysId option").remove();
			$.each(reses, function(i,res){
				var tmp = "<option value='"+res.s1+"'>"+res.s2+"</option>";
				$("select#subsysId").append(tmp);
			});
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	changeSubsys();
}

function changeSubsys() {
	$.ajax({type:"post",url:"QueryFTPconnectionInfo",dataType: "json",async:false,
		data:{subsysId:$("select#subsysId option:selected").val()},
		success : function(reses) {
			if( reses.length >0 ) {
				$("input[type='text']").attr("disabled",false);
				$("select#ftpIP option").remove();
				$.each(reses, function(i,res){
					var tmp = "<option path='"+res.s4+"' usr='"+res.s2+"' pwd='"+res.s3+"' value="+res.s5+">"+res.s1+"</option>";
					$("select#ftpIP").append(tmp);
					if( i==0 ) {
						$("input:first").val( res.s2 ).prop("size",(res.s2).length);
						$("input:eq(1)").val( res.s3 ).prop("size",(res.s3).length*2);
						$("input:eq(2)").val( res.s4 ).prop("size",(res.s4).length);
					}
				});
			} else {
				$("input[type='text']").attr("disabled",true);
			}
			
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
}

function changeFTPinf() {
	var usr = $("select#ftpIP option:selected").attr("usr");
	var pwd = $("select#ftpIP option:selected").attr("pwd");
	var path = $("select#ftpIP option:selected").attr("path");
	var value = $("select#ftpIP option:selected").val();

	$("input:first").val( usr ).prop("size",usr.length );
	$("input:eq(1)").val( pwd ).prop("size",pwd.length );
	$("input:eq(2)").val( path ).prop("size",path.length );
	
	if( value=="5" ) { //5是目前約定要從開發Team那裡
		$("#tbodyFile tr").each(function(){
			if( $(this).children("td:eq(1)").html() == "forder-batch.war" ) {
				$(this).children("td:last").children("input").val( "forder-batch-jboss.war" );
			} else if( $(this).children("td:eq(1)").html() == "forder-ear.ear" ) {
				$(this).children("td:last").children("input").val( "forder-ear-1.0.0-jboss.ear" );
			}
		});
		
		$("#tbodyReqs tr").remove();
		var tmp="<tr class='req'><td class='center engN'><button type='button' onclick='removeReq(this)'>-</button></td><td class='idx int center'>"+($("#tbodyReqs tr").size()+1)+"</td><th><input class='engN' type='text' value='VE_Routine' size='14'/></th><th class='zhTwN'><textarea>驗證區例行性部署</textarea></th></tr>"
		$("#tbodyReqs").append(tmp);
		$("#tbodyReqs button").button();
			
		$("textarea#comment").val("驗證區例行性部署");
	}
}

$(document).ready(function() {
	init("sdel.html");
	
	$.ajax({type:"post",url:"GetSystemList",dataType: "json",async:false,
		success : function(reses) {
			$("#targetSystem option").remove();
			$.each(reses, function(i,res){
				var tmp = "<option value='"+res.i1+"'>"+res.s1+"</option>";
				$("#targetSystem").append(tmp);
			});
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	//取得交付對象下相關的子系統
	$.ajax({type:"post",url:"QuerySys2Subsys",dataType: "json",async:false,
		data:{sysSN:$("#targetSystem option:selected").val()},
		success : function(reses) {
			$("select#subsysId option").remove();
			$.each(reses, function(i,res){
				var tmp = "<option value='"+res.s1+"'>"+res.s2+"</option>";
				$("select#subsysId").append(tmp);
			});
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	
	changeSubsys();
	
	$.ajax({type:"post",url:"QueryFileCandidates",dataType: "json",async:false,
		success : function(reses) {
			$.each(reses, function(i,res){
				var tmp = "<tr><td class='int'>"+(i+1)+".</td><td class='engN'>"+res+"</td><td class='left'><input depFile='"+res+"' class='engN fileInfo' type='text'/></td></tr>"
				$("#tbodyFile").append(tmp);
			});
			refreshUI(1,"tbodyFile",false);
		},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
	});
	
	$("#btnSubmit").bind("click", function(){
		clearErrorMsg();
		
		windowFreeze();
		/*
			先檢查必要的資訊都有輸入
			1. 連線資訊皆有輸入
			2. 起碼有一個檔案
			3. 起碼有一個需求
		*/
		var isFTPconnectionInfo = true;
		$(".ftp").each(function(){
			var tmp = $.trim($(this).val());
			if ("" == tmp) {
				isFTPconnectionInfo = false;
			}
		});
		
		var isReqExisted = false;
		var isNotBreak = 0;
		$("tr.req").each(function(){	//檢查有沒有輸入需求
			var reqSID = $(this).children("th:first").children("input").val();
			var reqDesc = $(this).children("th:last").children("textarea").val();
			
			if( $.trim(reqSID) !="" && $.trim(reqDesc) !="" ) {
				isReqExisted = true;
				$.ajax({type:"post",url:"IsReqDuplicate",dataType: "text",async:false,
					data:{reqSID:reqSID,reqDesc:reqDesc},
					success : function(res) {
						if ( res==2 ) {	//reqSID已存在，但需求描述不是所輸入的
							isNotBreak = 2;
						}
					},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
				});
				
			} else if( $.trim(reqSID) =="" && $.trim(reqDesc) =="" ) {
				$(this).remove();
			} else {
				isNotBreak = 1;
			}
		});
		
		if( isNotBreak ==0 ) {
			var isFilesInfo = false;
			if ( isFTPconnectionInfo ) {
				$(".fileInfo").each(function(){
					var tmp = $.trim($(this).val());
					if ("" != tmp) {
						isFilesInfo = true;
					}
				});
				if ( isFilesInfo ) {	//該有的資訊都有了
					
					if( isReqExisted ) {
						var isFTPconnectOK = false;
						var isFileOnFTP = true;
						var ftpMessage = "";
						$.ajax({type:"post",url:"ChkFTPconnection",dataType: "text",async:false,
							data:{url:$("#ftpIP option:selected").text(),usr:$("#ftpID").val(),pwd:$("#ftpPWD").val(),path:$("#ftpPATH").val()},
							success : function(res) {
								if ( res==4 ) {
									isFTPconnectOK = true;
								} else if ( res==3 ) {
									ftpMessage="連線帳密有誤";
								} else if ( res==2 ) {
									ftpMessage="無法連線至主機";
								} else if ( res==5 ) {
									ftpMessage="FTP上不存在指定的路徑";
								}
							},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
						});
						
						if ( isFTPconnectOK ) {	//連線正常，開始確認檔案是否存在
							$(".fileInfo").each(function(){
								var tmp = $.trim($(this).val());
								if ("" != tmp) {
									$.ajax({type:"post",url:"IsFileOnFTP",dataType:"text",async:false,
										data:{url:$("#ftpIP option:selected").text(),usr:$("#ftpID").val(),pwd:$("#ftpPWD").val(),path:$("#ftpPATH").val(),fileName:tmp},
										success : function(res) {
											if ( res!=4 ) {
												isFileOnFTP = false;
											} 
										},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
									});
								}
							});
						
							if ( isFileOnFTP ) {	//檔案存在，該有的資訊也有，確認需求有輸入
								
								var ftpURL = $("#ftpIP option:selected").text();
								var ftpID = $("#ftpID").val();
								var ftpPWD = $("#ftpPWD").val();
								var ftpPATH = $("#ftpPATH").val();
								var comment = $("textarea#comment").val();
								var files = [];
								var reqs = [];
								
								$(".fileInfo").each(function(){
									var file = $.trim($(this).val());
									var depFile = $(this).attr("depFile");
									files.push({"s1":depFile,"s2":file});	
								});
								
								$("tr.req").each(function(){
									var reqSID = $(this).children("th:first").children("input").val();
									var reqDesc = $(this).children("th:last").children("textarea").val();
									reqs.push({"reqSID":reqSID,"reqDesc":reqDesc});
								});
								
								var fOrderDeliver = {"comment":comment,"ftpURL":ftpURL,"ftpID":ftpID,"ftpPWD":ftpPWD,"ftpPATH":ftpPATH,"files":files,"reqs":reqs };
								
								
								$.ajax({type:"post",url:"Deliver4SDEL_2",dataType: "text",async:false,
									data:{isFTP:true,fOrderDeliver:JSON.stringify(fOrderDeliver),localSystemId:$.cookie("localSystemId"),localSystemName:$.cookie("localSystemName"),empId:$.cookie("empId"),empName:$.cookie("empName"),subsysId:"fOrder",targetSystemId:$("#targetSystem option:selected").attr("value")},
									success : function(res) {
										var tmp = res;
										if( tmp == "OK" ) {
											windowUnfreeze();
											alert("成功");
										} else {
											$("#errShortMsg").html("出現錯誤");
											$("#errLongMsg").html(tmp);
										}
									},error : function(xhr, ajaxOptions, thrownError) { errorAjx(xhr); }
								});
							} else {
								$("#errShortMsg").html("檔案不存在");
							}
						} else {
							$("#errShortMsg").html(ftpMessage);
						}
					} else {
						$("#errShortMsg").html("至少輸入一筆需求");
					}
				} else {
					$("#errShortMsg").html("起碼要輸入一個檔案");
				}
			} else {
				$("#errShortMsg").html("FTP連線資訊有遺漏");
			}
		} else if (isNotBreak==1) {
			$("#errShortMsg").html("需求資訊輸入不完整");
		} else if (isNotBreak==2) {
			$("#errShortMsg").html("這個需求編號，和以往輸入的修改內容不同");
		}
		windowUnfreeze();
	});
	
	$("#btnAddReq").bind("click",function(){
		var tmp="<tr class='req'><td class='center engN'><button type='button' onclick='removeReq(this)'>-</button></td><td class='idx int center'>"+($("#tbodyReqs tr").size()+1)+"</td><th><input class='engN' type='text' size='14'/></th><th class='zhTwN' cols='100'><textarea></textarea></th></tr>"
		$("#tbodyReqs").append(tmp);
		refreshUI(1,"tbodyReqs",false);
	});
	
	
	$("button").button();
});
</script>

</head>
<body>
	<div id="header"></div>
	
	<p>
	<img src="pic/delivery.png" > 
	<p>
	
	<button type="button" id="btnSubmit">送出</button>
	
	<p>
	
	
	
	
	<table id="tableFile">
		<thead class="tableFirst">
		<!-- 
		<tr><th class="zhTwN">註解：</th><th colspan="2" class="zhTwN right">交付目標：</th><td class="left"><SELECT onchange="changeTargetSys()" id="targetSystem" class="zhTwN"></SELECT></td></tr>
			<tr><td rowspan="5"><textarea id="comment" rows="6" class="zhTwN"></textarea></td><th colspan="2" class="zhTwN right">交付系統：</th><td class="left"><select id="subsysId"></select></td></tr>
			<tr><th colspan="2" class="zhTwN right">SVN主機：</th><td class="left"><SELECT id="svnHost" onchange="changeSVNinf()" class="zhTwN"></SELECT></td></tr>
			<tr><th rowspan="3" class="zhTwN center">SVN相關</th><th class="engN right">URL：</th><td class="left"><input id="ftpURL" class="engN ftp" type="text" size="14" maxlength="15" /></td></tr>
			<tr><th class="engN right">帳號：</th><td class="left"><input id="svnID" class="engN ftp" type="text"/></td></tr>
			<tr><th class="engN right">密碼：</th><td class="left"><input id="svnPWD" class="ftp" type="password"/></td></tr>
		
		 -->
		
		<tr><th class="zhTwN">註解：</th><th colspan="2" class="zhTwN right">交付目標：</th><td class="left"><SELECT onchange="changeTargetSys()" id="targetSystem" class="zhTwN"></SELECT></td></tr>
		<tr><td rowspan="5"><textarea id="comment" rows="6" class="zhTwN"></textarea></td><th colspan="2" class="zhTwN right">交付系統：</th><td class="left"><select onchange="changeSubsys()" id="subsysId"></select></td></tr>
		<tr><th colspan="2" class="zhTwN right">FTP主機：</th><td class="left"><SELECT id="ftpIP" onchange="changeFTPinf()" class="zhTwN"></SELECT></td></tr>
		<tr><th rowspan="3" class="zhTwN center">FTP相關</th><th class="engN right">登入帳號：</th><td class="left"><input id="ftpID" class="engN ftp" type="text"/></td></tr>
		<tr><th class="engN right">登入密碼：</th><td class="left"><input id="ftpPWD" class="ftp" type="password"/></td></tr>
		<tr><th class="zhTwN right">路徑：</th><td class="left"><input id="ftpPATH" class="engN ftp" type="text"/></td></tr>
		</thead>
		
	</table>
	<p><p>
	<table id="tableSVNF">
		<thead class="tableFirst">
			<tr><th class="engN">#</th><th class="zhTwN">上版後名稱</th><th class="zhTwN">FTP上檔案名</th></tr>
		</thead>
		<tbody id="tbodyFile" class="tableFirst"></tbody>
	</table>
	
	
	<p>	
	<table id="tableReqs">
		<thead class="tableFirst">
			<tr><td class="center engN"><button type="button" id="btnAddReq">+</button></td><th class="engN">#</th><th class="zhTwN">需求編號</th><th class="zhTwN">修改內容</th></tr>
		</thead>
		<tbody id="tbodyReqs"></tbody>
	</table>
</body>
</html>